package com.lee.spring.jdbc.query;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;

/**
 * 解决jdbcTemplate.queryForObject 时，数据集为空是抛出异常的问题
 * @author xiangke
 *
 */
public class IJdbcTemplate extends JdbcTemplate {
	
	@Override
	public <T> T queryForObject(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException {
		List<T> results = query(sql, args, new RowMapperResultSetExtractor<T>(
				rowMapper, 1));
		return IDataAccessUtils.requiredSingleResult(results);
	}
}